<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="维修单号" prop="repairCode">
        <el-input v-model="queryParams.repairCode" placeholder="请输入维修单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工单编号" prop="workorderCode">
        <el-input v-model="queryParams.workorderCode" placeholder="请输入工单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="维修开始时间" prop="repairStartTime" label-width="100px">
        <el-date-picker clearable v-model="queryParams.repairStartTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择维修开始时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="维修结束时间" prop="repairEndTime" label-width="100px">
        <el-date-picker clearable v-model="queryParams.repairEndTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择维修结束时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="维修人" prop="repairBy">
        <el-input v-model="queryParams.repairBy" placeholder="请输入维修人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="维修来源" prop="repairSource">
        <el-select v-model="queryParams.repairSource" placeholder="请选择维修来源" style="width: 360px">
          <el-option v-for="item in repairSourceList" :key="item.label + item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="SN编码" prop="snCode">
        <el-input v-model="queryParams.snCode" placeholder="请输入SN编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物料名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入物料名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['produce:repair:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['produce:repair:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['produce:repair:export']">导出</el-button>
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['produce:repair:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pro:repair:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pro:repair:export']"
        >导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange" height="calc(100vh - 230px)">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="维修单号" align="center" prop="repairCode" width="140px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)">{{ scope.row.repairCode }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="维修来源" align="center" prop="repairSourceName" width="80px" />
      <el-table-column label="异常描述" align="center" prop="exceptionDescription" width="120px" />
      <el-table-column label="维修人" align="center" prop="repairBy" />
      <el-table-column label="维修开始时间" align="center" prop="repairStartTime" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.repairStartTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修结束时间" align="center" prop="repairEndTime" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.repairEndTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修时长" align="center" prop="formatMinutes" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.formatMinutes }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工单编号" align="center" prop="workorderCode" width="110px" />
      <el-table-column label="SN编码" align="center" prop="snCode" width="110px" />
      <el-table-column label="类型" align="center" prop="orderType" width="80px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_type" :value="scope.row.orderType" />
        </template>
      </el-table-column>
      <el-table-column label="物料编码" align="center" prop="productCode" width="110px" />
      <el-table-column label="物料名称" align="center" prop="productName" width="110px" />
      <el-table-column label="规格型号" align="center" prop="productSpec" width="130px" />
      <el-table-column label="工序编号" align="center" prop="processCode" width="120px" />
      <el-table-column label="工序名称" align="center" prop="processName" width="100px" />
      <el-table-column label="维修原因" align="center" prop="repairReason" width="200px" />
      <el-table-column label="维修状态" align="center" prop="status" width="80px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_equip_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="130px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['produce:repair:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['produce:repair:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改设备维修对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" size="small">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="维修单号" prop="repairCode">
              <el-input v-model="form.repairCode" placeholder="请输入维修单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单编号" prop="workorderCode">
              <el-input v-model="form.workorderCode" placeholder="请输入工单编号" @keyup.enter.native="handleGetWorkorder" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="数量" prop="quantity">
              <el-input v-model="form.quantity" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="SN编码" prop="snCode">
              <el-input v-model="form.snCode" placeholder="请输入SN编码" id="scanner-input" @input="snbutton" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="异常描述" prop="exceptionDescription">
              <el-input type="textarea" v-model="form.exceptionDescription" placeholder="请输入异常描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维修原因" prop="repairReason">
              <el-input type="textarea" v-model="form.repairReason" placeholder="请输入维修原因" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="维修开始时间" prop="repairStartTime">
              <el-date-picker disabled style="width: 360px" clearable v-model="form.repairStartTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择维修开始时间"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维修结束时间" prop="repairEndTime">
              <el-date-picker disabled style="width: 360px" clearable v-model="form.repairEndTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择维修结束时间"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="物料编码" prop="productCode">
              <el-input v-model="form.productCode" placeholder="请输入物料编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="规格型号" prop="productSpec">
              <el-input v-model="form.productSpec" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维修来源" prop="repairSource">
              <el-select v-model="form.repairSource" placeholder="请选择维修来源" style="width: 360px">
                <el-option v-for="item in repairSourceList" :key="item.label + item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="维修状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择维修状态" style="width: 360px">
                <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调试模板" prop="qcTemplateId ">
              <el-select v-model="form.qcTemplateId" placeholder="请选择调试模板" style="width: 360px">
                <el-option v-for="item in statusList2" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!--  add begin by zyj 2024/11/21 增加录入更换物料列表 -->
        <el-button @click="showSelectMaterialDialog">新增更换物料</el-button>
        <el-table :data="form.changeMaterialList"> }}
          <el-table-column label="物料编码" width="120" align="center" key="itemCode" prop="itemCode">
          </el-table-column>
          <el-table-column label="物料名称" align="center" key="itemName" prop="itemName" :show-overflow-tooltip="true" />
          <el-table-column label="规格型号" width="210" align="center" key="specification" prop="specification" :show-overflow-tooltip="true" />
          <el-table-column label="单位" align="center" prop="unitOfMeasure" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="数量" align="center" prop="cnt" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-form-item size="large" label-width="0" :prop="'changeMaterialList['+scope.$index+'].cnt'" :rules="[{required: true, message: '请输入数量', trigger: 'blur'},{ type: 'number', message: '请输入正确的数量', trigger: ['blur', 'change'] }]">
                <el-input size="large" v-model.number="form.changeMaterialList[scope.$index].cnt" type="number" placeholder="请输入数量"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" prop="remark" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-button type="danger" size="mini" @click="deleteRow(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <br />
        <!--  add end by zyj 2024/11/21 增加录入更换物料列表 -->
      </el-form>

      <div class="scopedTab">
        <!-- :span-method="objectSpanMethod" -->
        <el-table :data="repairList2" :cell-style="{ background: '#f5f5f5', borderColor: '#bfbfbf' }" height="300px" :span-method="objectSpanMethod">
          <el-table-column label="序号" align="center" width="60px">
            <template slot-scope="scope">
              <span>{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column label="测试步骤" align="center" prop="dictLabel" width="150px" />
          <el-table-column label="测试项目" align="center" prop="indexName" width="200px" />
          <el-table-column label="测试标准" align="center" prop="qcStandard" width="360px" />
          <el-table-column label="测试结果" align="center" prop="orderType" width="170px">
            <template v-slot="scope">
              <!-- 单选 -->
              <div v-if="scope.row.optType != 1">
                <el-radio v-model="scope.row.checkResult" label="1">√</el-radio>
                <el-radio v-model="scope.row.checkResult" label="0">×</el-radio>
              </div>
              <!-- 文本框 -->
              <div v-if="scope.row.optType != 0">
                <el-input v-model="scope.row.checkValue" type="text" style="width: 120px" />
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-drawer title="设备维修详情" :visible.sync="openDetail" size="80%" @close="cancel">
      <div style="padding: 0 20px">
        <el-tag size="large" style="font-size: 20px">基本信息</el-tag>
        <hr />
        <el-row :gutter="20">
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>设备维修编码：</span>
              <span>{{ formDetail.repairCode }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>设备名称：</span>
              <span>{{ formDetail.equipmentName }}</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>异常描述信息：</span>
              <span>{{ formDetail.exceptionDescription }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>维修人：</span>
              <span>{{ formDetail.repairBy }}</span>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>维修开始时间：</span>
              <span>{{ formDetail.repairStartTime }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>维修结束时间：</span>
              <span>{{ formDetail.repairEndTime }}</span>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>工单编号：</span>
              <span>{{ formDetail.workorderCode }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>工序编号：</span>
              <span>{{ formDetail.processCode }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>工序名称：</span>
              <span>{{ formDetail.processName }}</span>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>物料编码：</span>
              <span>{{ formDetail.productCode }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>物料名称：</span>
              <span>{{ formDetail.productName }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>规格型号：</span>
              <span>{{ formDetail.productSpec }}</span>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>车间名称：</span>
              <span>{{ formDetail.workshopName }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>产线名称：</span>
              <span>{{ formDetail.lineName }}</span>
            </div>
          </el-col>
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>工位名称：</span>
              <span>{{ formDetail.workstationName }}</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="10">
            <div class="grid-content bg-purple">
              <span>备注：</span>
              <span>{{ formDetail.remark }}</span>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <span>维修原因：</span>
              <span>{{ formDetail.repairReason }}</span>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <div class="grid-content bg-purple">
              <span>调试模板：</span>
              <span>{{ setStatusList(formDetail.qcTemplateId) }}</span>
            </div>
          </el-col>
        </el-row>
        <div class="scopedTab">
          <el-table :data="repairList2" :cell-style="{ background: '#f5f5f5', borderColor: '#bfbfbf' }" height="500px" :span-method="objectSpanMethod">
            <el-table-column label="序号" align="center" width="60px">
              <template slot-scope="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column label="测试步骤" align="center" prop="dictLabel" width="300px" />
            <el-table-column label="测试项目" align="center" prop="indexName" width="330px" />
            <el-table-column label="测试标准" align="center" prop="qcStandard" width="400px" />
            <el-table-column label="测试结果" align="center" prop="orderType" width="230px">
              <template v-slot="scope">
                <!-- 单选 -->
                <div v-if="scope.row.optType != 1">
                  <el-radio disabled v-model="scope.row.checkResult" label="1">√</el-radio>
                  <el-radio disabled v-model="scope.row.checkResult" label="0">×</el-radio>
                </div>
                <!-- 文本框 -->
                <div v-if="scope.row.optType != 0">
                  <el-input disabled v-model="scope.row.checkValue" type="text" style="width: 120px" />
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-drawer>
    <!-- add begin by zyj 2024/11/21 增加录入更换物料列表 -->
    <SelectMaterialDialog ref="selectMaterialDialogRef" @onSelection="mdItemSelected"></SelectMaterialDialog>
    <!-- add end by zyj 2024/11/21 增加录入更换物料列表 -->
  </div>
</template>

<script>
import {
  listRepair,
  getRepair,
  delRepair,
  addRepair,
  updateRepair,
  getWorkorder,
  getTemplate,
  getTemplateList,
  getInfoBySnCode,
  delRepairMaterial
} from "@/api/mes/pro/repair";
import SelectMaterialDialog from "./components/SelectMaterialDialog.vue";
import moment from "moment";
export default {
  name: "Repair",
  components: {
    SelectMaterialDialog,
  },
  dicts: ["mes_workorder_status", "mes_order_type", "mes_equip_status"],

  data() {
    return {

      // 遮罩层
      loading: true,
      loading2: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备维修表格数据
      repairList: [],
      // 设备维修表格数据
      repairList2: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      mergeObj: {}, // 用来记录需要合并行的下标
      mergeArr: ["dictLabel", "indexName", "qcStandard"],
      // 查询参数
      queryParams: {
        repairCode: null,
        pageNum: 1,
        pageSize: 10,
        repairCode: null,
        equipmentId: null,
        equipmentName: null,
        exceptionDescription: null,
        repairBy: null,
        repairStartTime: null,
        repairEndTime: null,
        workorderId: null,
        workorderCode: null,
        orderType: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpec: null,
        lineId: null,
        processId: null,
        processName: null,
        processCode: null,
        status: null,
        repairBy: null,
        productName: null,
        snCode: null,
      },
      // 表单参数
      form: {
        quantity: 1,
        //add begin by zyj 2024/11/21 增加录入更换物料列表
        changeMaterialList: [],
        //add end by zyj 2024/11/21 增加录入更换物料列表
      },
      formDetail: {},
      repairSourceList: [
        {
          label: "售后",
          value: 1,
        },
        {
          label: "产线",
          value: 2,
        },
        {
          label: "不良品",
          value: 3,
        },
        {
          label: "电桩",
          value: 4,
        },
        {
          label: "车载",
          value: 5,
        },
        {
          label: "品质(调试)",
          value: 6,
        },
      ],
      statusList: [
        {
          label: "已维修",
          value: "0",
        },
        {
          label: "待维修",
          value: "1",
        },
        {
          label: "无法维修",
          value: "2",
        },
      ],
      statusList2: [],
      // 表单校验
      rules: {
        quantity: [
          { required: true, message: "数量不能为空", trigger: "blur" },
        ],
        snCode: [
          { required: true, message: "SN编码不能为空", trigger: "blur" },
        ],
        exceptionDescription: [
          { required: true, message: "异常描述不能为空", trigger: "blur" },
        ],
        repairReason: [
          { required: true, message: "维修原因不能为空", trigger: "blur" },
        ],
        repairStartTime: [
          { required: true, message: "维修开始时间不能为空", trigger: "blur" },
        ],
        repairEndTime: [
          { required: true, message: "维修结束时间不能为空", trigger: "blur" },
        ],
        repairSource: [
          { required: true, message: "维修来源不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "维修状态不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    "form.qcTemplateId"(newVal, oldVal) {
      if (!newVal) return;
      this.getTemplate(newVal);
    },
    "formDetail.qcTemplateId"(newVal, oldVal) {
      if (!newVal) return;
      this.getTemplate(newVal);
    },
  },
  created() {
    this.getList();
    this.getSelections();
  },
  methods: {
    // add begin by zyj 2024/11/21 增加录入更换物料列表 
    deleteRow(index) {
      this.$confirm("确定要删除该行吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (this.form.changeMaterialList[index].id) {
          delRepairMaterial(this.form.changeMaterialList[index].id).then(res => {
            this.$message.success("删除成功");
            this.form.changeMaterialList.splice(index, 1);
          }).catch(err => {
            this.$message.warning("删除失败");
          })
        } else {
          this.form.changeMaterialList.splice(index, 1);
          this.$message.success("删除成功");
        }
      });
    },
    showSelectMaterialDialog() {
      console.log("this.form.changeMaterialList", this.form.changeMaterialList)
      this.$refs.selectMaterialDialogRef.init(this.form.changeMaterialList);
    },
    mdItemSelected(mdItems) {
      let mdItemsCopy = JSON.parse(JSON.stringify(mdItems));
      mdItemsCopy = mdItemsCopy.filter(item => this.form.changeMaterialList.length == 0 || this.form.changeMaterialList.filter(subItem => subItem.itemCode == item.itemCode).length == 0);
      mdItemsCopy.forEach(item => {
        item.cnt = 0;
      })
      this.form.changeMaterialList.push(...mdItemsCopy);
      this.$forceUpdate()
    },
    // add end by zyj 2024/11/21 增加录入更换物料列表 
    snbutton(e) {
      var myInputOrder = document.getElementById("scanner-input");
      myInputOrder.onkeydown = (even) => {
        console.log(even, "=====e=====");

        if (even.key == "Enter") {
          //赋值给v-model
          console.log(this.form.snCode, this.form);

          getInfoBySnCode(this.form.snCode).then((res) => {
            console.log("查询到", res.data || res.data);
            if (!res.data) return;
            this.form["productSpec"] = res.data["productSpec"];
            this.form["productName"] = res.data["productName"];
            this.form["productCode"] = res.data["productCode"];
            this.form["workorderCode"] = res.data["workorderCode"];
          });
          if (this.title == "修改设备维修") return;
          this.form.repairStartTime = moment().format("YYYY-MM-DD HH:mm:ss");
        }
      };
    },

    //获取下拉
    setStatusList(code) {
      let str = "";
      this.statusList2.forEach((item) => {
        if (item.value == code) {
          str = item.label;
        }
      });
      return str;
    },
    getTemplate(code) {
      console.log("code", code)
      getTemplateList({ templateId: code }).then((res) => {
        if (res.code != 200) return this.$message.error("模板列表请求失败");
        let arr = res.data.map((item) => ({
          ...item,
          checkResult: null,
          checkValue: null,
        }));

        this.getSpanArr(arr);
        debugger
        this.repairList2 = arr;

      });
    },
    getSpanArr(data) {
      this.mergeArr.forEach((key, index1) => {
        let count = 0; // 用来记录需要合并行的起始位置
        this.mergeObj[key] = []; // 记录每一列的合并信息
        data.forEach((item, index) => {
          // index == 0表示数据为第一行，直接 push 一个 1
          if (index === 0) {
            this.mergeObj[key].push(1);
          } else {
            // 判断当前行是否与上一行其值相等 如果相等 在 count 记录的位置其值 +1 表示当前行需要合并 并push 一个 0 作为占位
            if (item[key] === data[index - 1][key]) {
              this.mergeObj[key][count] += 1;
              this.mergeObj[key].push(0);
            } else {
              // 如果当前行和上一行其值不相等
              count = index; // 记录当前位置
              this.mergeObj[key].push(1); // 重新push 一个 1
            }
          }
        });
      });
      console.log("this.mergeObj", this.mergeObj)
    },
    // objectSpanMethod方法
    // 默认接受四个值 { 当前行的值, 当前列的值, 行的下标, 列的下标 }
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      console.log(row, column, rowIndex, columnIndex);
      // 判断列的属性
      if (this.mergeArr.indexOf(column.property) !== -1) {
        // 判断其值是不是为0
        if (!this.mergeObj[column.property]) {
          return [1, 1];
        }
        if (this.mergeObj[column.property][rowIndex]) {
          return [this.mergeObj[column.property][rowIndex], 1];
        } else {
          // 如果为0则为需要合并的行
          return [0, 0];
        }
      }
    },

    getSelections() {
      getTemplate({ pageSize: 10000, qcTypes: "PCBA" }).then((res) => {
        this.statusList2 = res.rows.map((item) => ({
          label: item.templateName,
          value: item.templateId,
        }));
      });
    },
    handleGetWorkorder() {
      let param = {
        workorderCode: this.form.workorderCode,
      };
      getWorkorder(param).then((res) => {
        if (res && res.rows && res.rows.length) {
          const data = res.rows[0];
          this.form.productCode = data.productCode;
          this.form.productName = data.productName;
          this.form.productSpec = data.productSpc;
        }
      });
    },
    /** 查询设备维修列表 */
    getList() {
      this.loading = true;
      listRepair(this.queryParams).then((response) => {
        for (var i = 0; i < response.rows.length; i++) {
          for (var j = 0; j < this.repairSourceList.length; j++) {
            if (
              this.repairSourceList[j].value == response.rows[i].repairSource
            ) {
              response.rows[i].repairSourceName =
                this.repairSourceList[j].label;
            }
          }
        }
        this.repairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openDetail = false;
      this.open = false;
      this.reset();
      this.repairList2 = [];
      this.form.changeMaterialList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        repairCode: null,
        equipmentId: null,
        equipmentName: null,
        exceptionDescription: null,
        repairBy: null,
        repairStartTime: null,
        repairEndTime: null,
        workorderId: null,
        workorderCode: null,
        orderType: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpec: null,
        lineId: null,
        processId: null,
        processName: null,
        processCode: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        changeMaterialList: [],
        updateBy: null,
        updateTime: null,
        quantity: 1,
      };
      this.formDetail = {};
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备维修";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.repairList2 = null;
      this.reset();
      const id = row.id || this.ids;
      getRepair(id).then((response) => {
        this.form = response.data;
        // this.form['cahngeMaterialList'] = []
        this.getSpanArr(response.data.qcPcbaInspectionItems);
        this.repairList2 = response.data.qcPcbaInspectionItems;
        const newArr = response.data.qcPcbaInspectionItems;
        /*  */
        setTimeout(() => {
          this.repairList2 = newArr.map((item, index) => ({
            ...item,
            id: newArr[index].id + "",
            checkResult: newArr[index].checkResult + "",
            checkValue: newArr[index].checkValue,
          }));
          this.open = true;
        }, 500);

        // response.data.qcPcbaInspectionItems.map

        this.title = "修改设备维修";
      });
    },
    handleView(row) {
      this.getSelections();
      this.reset();
      const id = row.id || this.ids.join(",");
      getRepair(id).then((response) => {
        // this.form = response.data;
        this.formDetail = response.data;
        this.getSpanArr(this.formDetail.qcPcbaInspectionItems);

        setTimeout(() => {
          const newArr = this.formDetail.qcPcbaInspectionItems;
          this.repairList2 = this.formDetail.qcPcbaInspectionItems;
          this.repairList2 = newArr.map((item, index) => ({
            ...item,
            id: newArr[index].id + "",
            checkResult: newArr[index].checkResult + "",
            checkValue: newArr[index].checkValue,
          }));
          this.openDetail = true;
        }, 500);
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.title != "修改设备维修") {
        this.form.repairEndTime = moment().format("YYYY-MM-DD HH:mm:ss");
      }
      this.$refs["form"].validate((valid) => {
        const qcPcbaInspectionItems = this.repairList2.map((item) => {
          console.log(item);
          return {
            id: item.id || null,
            qcTemplateId: item.templateId || null,
            indexId: item.indexId || null,
            checkResult: item.checkResult,
            checkValue: item.checkValue,
          };
        });
        if (valid) {
          if (this.form.id != null) {
            updateRepair({ ...this.form, qcPcbaInspectionItems }).then(
              (response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            );
          } else {
            addRepair({ ...this.form, qcPcbaInspectionItems }).then(
              (response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            );
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除设备维修编号为"' + ids + '"的数据项？')
        .then(function () {
          return delRepair(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/repair/export",
        JSON.stringify({
          ...this.queryParams,
        }),
        `repair_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-container ::v-deep .el-drawer__header > :first-child {
  font-weight: 700 !important;
  font-size: 20px;
}
.drawerFooter {
  text-align: right;
  padding: 20px;
}
.bg-purple {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    display: inline-block;
    //width: 110px;
    white-space: nowrap;
    line-height: 30px;
  }
}
::v-deep .el-table tbody tr:hover > td {
  background-color: #f5f5f5 !important;
}

.scopedtab ::v-deep .el-table th {
  border: 1px solid #000;
}
.scopedtab ::v-deep.el-table td {
  border: 1px solid #000;
}
/**改变表头标题颜色*/
.scopedtab ::v-deep.el-table thead {
  color: #000;
}

.scopedTab ::v-deep .el-table td.el-table__cell {
  border: 1px solid #d9d8d2 !important;
}
</style>
